#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=100010;
int n,m;
int T;
ll _a[N],c[N],a[N];
vector <int> d[50];
int now[N],idx,f[N];
ll up[N],down[N];
vector <int> p[N];
ll calc(ll x){
    ll k=0,t=1;
    while(t<x) t*=2,k++;
    return pow(2,k);
}
ll res;
void work(ll dep,int lim,int n){
    // cout<<"N: "<<n<<endl;
    // for(int i=1;i<=n;i++){
    //     for(auto y:p[i]){
    //         cout<<y<<' '<<up[y]<<' '<<down[y]<<endl;
    //     }
    //     cout<<endl;
    // }
    // cout<<endl;
    if(n==1){
        for(auto y:p[1]){
            // cout<<y<<' ';
            res+=y;
        }
        // cout<<endl;
        return;
    }
    vector <int> t;
    for(int i=1;i<=lim;i++) f[i]=0;
    for(int i=1,tim=0;i<=n;i+=2,tim++){
        t.clear();
        for(auto z:p[i+1])
            for(auto y:p[i]){
                // cout<<z<<' '<<y<<endl;
                // cout<<up[z]<<' '<<up[y]<<endl;
                if(d[dep][tim]){
                    if(up[z]>dep){
                        // cout<<z<<endl;
                        if(!f[z]) t.push_back(z);
                        f[z]=1;
                    }
                }
                else{
                    if(down[y]<dep){
                        // cout<<z<<endl;
                        // down[z]=max(down[z],dep);
                        if(!f[z]) t.push_back(z);
                        f[z]=2;
                        break;
                    }
                }
            }
        for(auto y:p[i])
            for(auto z:p[i+1]){
                if(!d[dep][tim]){
                    if(up[y]>dep){
                        // cout<<y<<endl;
                        if(!f[y]) t.push_back(y);
                        f[y]=1;
                    }
                }
                else{
                    if(down[z]<dep){
                        // cout<<y<<endl;
                        // down[y]=max(down[y],dep);
                        if(!f[y]) t.push_back(y);
                        f[y]=2;
                        break;
                    }
                }
            }
        // cout<<f[7]<<' '<<f[8]<<endl;
        p[(i+1)/2].clear();
        for(auto y:t){
            if(f[y]==1) down[y]=max(down[y],dep);
            p[(i+1)/2].push_back(y);
        }
    }
    work(dep+1,lim,n/2);
}
void solve(){
    ll ans=0;
    for(int i=1;i<=m;i++){
        for(int j=1;j<=c[i];j++){
            p[j].clear();
            p[j].push_back(j);
            up[j]=a[j]+1;down[j]=a[j];
            idx=calc(c[i]);
            for(int k=c[i]+1;k<=idx;k++){
                p[k].clear();
                p[k].push_back(k);
                up[k]=2147483648;down[k]=0;
            }
        }
        res=0;
        // cout<<idx<<endl;
        work(1,idx,idx);
        ans=ans^(res*i);
    }
    cout<<ans<<endl;
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>_a[i];
    for(int i=1;i<=m;i++) cin>>c[i];
    ll k=0,t=1;
    while(t<n) t*=2,k++;
    char b;
    for(int i=1;i<=k;i++){
        t/=2;
        for(int j=1;j<=t;j++){
            cin>>b;
            d[i].push_back(b-'0');
        }
    }
    cin>>T;
    int X[7];
    while(T--){
        cin>>X[0]>>X[1]>>X[2]>>X[3];
        for(int i=1;i<=n;i++){
            a[i]=_a[i]^X[i%4];
        }
        solve();
    }
    return 0;
}
//0~40